package com.bsoft.gol.editor.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;


@Component
public class JSONDateUtil {
	private final static Logger log = LoggerFactory.getLogger(JSONDateUtil.class);

    public static String gdpathPrx = "";

    //正式环境请注释如下代码块，不然可能会抛异常
//    static {
//		if (IPUtil.cheakIPInLocalIPList("10.8.3.166") || IPUtil.cheakIPInLocalIPList("10.8.3.23")
//				|| IPUtil.cheakIPInLocalIPList("10.8.3.29")) {
//            //质控测试服务器
//            gdpathPrx = "/home/nethospital/3.0.1-master/txt/";
//        } else if (IPUtil.cheakIPInLocalIPList("10.0.38.78") || IPUtil.cheakIPInLocalIPList("10.0.38.49")
//        		|| IPUtil.cheakIPInLocalIPList("192.168.2.101") || IPUtil.cheakIPInLocalIPList("10.0.38.31")
//				|| IPUtil.cheakIPInLocalIPList("10.1.2.39")) {
//            //开发环境在后面加|| IPUtil.cheakIPInLocalIPList("您的IP")
//            gdpathPrx = JSONDateUtil.class.getResource("/").getPath();
//        }else if (IPUtil.cheakIPInLocalIPList("10.8.3.125")) {
//			//互联互通服务器
//			gdpathPrx = "D:/HLW/jsondata/";
//		}else if (IPUtil.cheakIPInLocalIPList("10.8.3.57")) {
//			//开发环境服务器
//			gdpathPrx = "D:/dev/data/";
//		}
//    }

	public static String readFile(String fileName) {
//		String pathPrx=new JSONDateUtil().getClass().getResource("/").getPath();
//		log.info("pathPrx:{}",pathPrx);
		String file=gdpathPrx+fileName+".txt";
		log.info("file:{}",file);
		BufferedReader reader = null;
		String laststr = "";
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
			reader = new BufferedReader(inputStreamReader);
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				laststr += tempString;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return laststr;
	}


	/**
	 * 读取体检问卷调查文件
	* @param @param fileName
	* @param @return    入参
	* @return String    返回类型
	* @author hujt   @date 2017年11月3日 下午1:00:32
	 */
	public static String readQuestionnaireFile(String fileName) {
		String file=fileName+".txt";
		BufferedReader reader = null;
		String laststr = "";
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
			reader = new BufferedReader(inputStreamReader);
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				laststr += tempString;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return laststr;
	}

}